package com.iwm.qa.mdm.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.gson.Gson;
import com.iwm.qa.mdm.R;
import com.iwm.qa.mdm.adapter.NormalRecyclerViewAdapter;
import com.iwm.qa.mdm.config.ApiConfig;
import com.iwm.qa.mdm.http.OkHttp;
import com.iwm.qa.mdm.model.Device;

import java.io.IOException;

import okhttp3.Request;

import static com.iwm.qa.mdm.view.AddEditDeviceActivity.EXTRA_ADD_DEVICE;
import static com.iwm.qa.mdm.view.AddEditDeviceActivity.EXTRA_DEVICE_OPER_TYPE;
import static com.iwm.qa.mdm.view.AddEditDeviceActivity.REQUEST_ADD_DEVICE;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private Device deviceBean;
    RecyclerView mRecyclerView;
    private NormalRecyclerViewAdapter recyclerViewAdapter;
    String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setRecyclerView();
        setOkHttp();
    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setImageResource(R.drawable.ic_add);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AddEditDeviceActivity.class);
                intent.putExtra(EXTRA_DEVICE_OPER_TYPE, EXTRA_ADD_DEVICE);
                startActivity(intent);
                Snackbar.make(view, "Saved Successfully !", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    public void setRecyclerView() {
        //创建默认的线性LayoutManager，类似于listview
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        recyclerViewAdapter = new NormalRecyclerViewAdapter(MainActivity.this);
        mRecyclerView.setAdapter(recyclerViewAdapter);
    }

    public void setOkHttp() {
        OkHttp.getAsync(ApiConfig.GET_ALL_DEVICE, new OkHttp.DataCallBack() {
            @Override
            public void requestFailure(Request request, IOException e) {

            }

            @Override
            public void requestSuccess(String result) throws Exception {
                Gson gson = new Gson();
                deviceBean = gson.fromJson(result, Device.class);
                if (recyclerViewAdapter != null) {
                    recyclerViewAdapter.setData(deviceBean);
                }
            }
        });
    }

    //获取结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_ADD_DEVICE) {
                setOkHttp();
            }
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            setOkHttp();
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            //设备list
            Intent device_main_intent = getIntent().setClass(MainActivity.this, MainActivity.class);
            startActivity(device_main_intent);
        } else if (id == R.id.nav_gallery) {
            //设备借用list
            Intent borrow_main_intent = getIntent().setClass(MainActivity.this, BorrowMainActivity.class);
            startActivity(borrow_main_intent);
        } else if (id == R.id.nav_slideshow) {
            //统计分析
        } else if (id == R.id.nav_manage) {
            //个人信息
        } else if (id == R.id.nav_share) {
            //登录
        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
